Laravel / Basics / Request flow
Request Flow
-
Step
1. In traditional approach, we can directly call and get the output of any file using file path , eg https:/domain.com/home.php, https:/domain.com/public/contactus.php etc
2. In MVC approach , we can directly call the files, but we cann't get the output , eg: https:/domain.com/app/Models/User.php or https:/domain.com/app/Http/Controllers/UserController.php 3. So we use routing in MVC approach1. MVC Routing
In MVC, routing is a process of mapping the browser request to the controller action and return response back.
1. When the request's URL matches any of the registered route patterns in the route table then the routing engine forwards the request to the appropriate handler for that request. Thereafter the route is processed and gets a view on the UI.
2. When the request's URL does not match any of the registered route patterns then the routing engine indicates that it could not determine how to handle the request by returning a 404 HTTP status code.
2. Request flow
1) In the above diagram, we can see many routes in the routes block , many actions in the controller and many views in the view block.
2) In the routes file, we define a url and set the relation to a controller and a function.
3) When a request comes, the routing mechanism check the requested url with url in the routes file. The routing mechanism forward the request to the corresponding controller and call the function which defined in the matching url(route)
4) In the controller , we return a view page
3.Laravel Sample code
in the above example, student is url. StudentController is controller. index is action in the StudentController. When we call the url 'student' in the browser address bar, the route 'student' in the routes file is processed and executed the function 'index' in the controller 'StudentController'
in the above example, student/grade is url. StudentController is controller. grade is action in the StudentController. When we call the url 'student/grade' in the browser address bar, the route 'student/grade' in the routes file is processed and executed the function 'grade' in the controller 'StudentController'
in the above example, student/fee is url. StudentController is controller. findFee is action in the StudentController. When we call the url 'student/fee' in the browser address bar, the route 'student/fee' in the routes file is processed and executed the function 'findGrade' in the controller 'StudentController'
in the above example, teacher is url. TeacherController is controller. index is action in the TeacherController. When we call the url 'teacher' in the browser address bar, the route 'teacher' in the routes file is processed and executed the function 'index' in the controller 'TeacherController'
in the above example, teacher/salary is url. TeacherController is controller. salary is action in the TeacherController. When we call the url 'teacher/salary' in the browser address bar, the route 'teacher/salary' in the routes file is processed and executed the function 'salary' in the controller 'TeacherController'
4. Next steps
1. how to create a route that points to laravel controller method
2. how to create a new controller class with method
3. how to create a view file